package com.example.myapplication.chap05;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

//【例5-6后一步骤】数据库记录的添加、查询、删除
public class DB3Activity extends AppCompatActivity {

    Button createdb,deletedb,queryBtn,deleteItemBtn,insertBtn;
    TextView txt;
    MySQLDatabase myc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diarydb);
        createdb = (Button)findViewById(R.id.createdb);
        deletedb = (Button)findViewById(R.id.deletedb);
        queryBtn =(Button)findViewById(R.id.querydb);
        deleteItemBtn = (Button)findViewById(R.id.deleteItemBtn);
        insertBtn = (Button)findViewById(R.id.insertdb);
        txt = (TextView)findViewById(R.id.txt);
        createdb.setOnClickListener(new mClick());
        deletedb.setOnClickListener(new mClick());
        queryBtn.setOnClickListener(new mClick());
        deleteItemBtn.setOnClickListener(new mClick());
        insertBtn.setOnClickListener(new mClick());
        myc = new MySQLDatabase();
    }
    class mClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if(view==createdb){
                myc.CreateDB(DB3Activity.this);
                txt.setText("创建数据库成功");
            }else if(view==deletedb){
                myc.deleteDB();
                txt.setText("删除数据库成功");
            }else if(view==queryBtn){
                txt.setText(myc.showItems());
            }else if(view==deleteItemBtn){
                myc.deleteItem();
                txt.setText("删除数据记录成功");
            }else if(view==insertBtn){
                myc.insertItem();
                txt.setText("添加数据记录成功");
            }
        }
    }
}
